/**
 * 类名：AccountAction
 * 创建日期： 2014-11-7 下午5:21:08
 * 
 */
package com.erms.action;

import com.erms.bean.User;
import com.erms.dao.DegreeDAO;
import com.erms.dao.DepartmentDAO;
import com.erms.dao.MajorDAO;
import com.erms.dao.UserDAO;
import com.erms.util.StringUtil;

/**
 * @author gwj 备注：
 * 
 */
public class AccountAction extends BaseAction {
	private UserDAO userDAO = null;
	private DepartmentDAO departmentDAO = null;
	private MajorDAO majorDAO = null;
	private DegreeDAO degreeDAO = null;
	private User user = null;
	private String password;
	private String email;
	private String phone;
	private String department;
	private String major;
	private String degree;
	private String dormitory;
	private String failReason = "";
	private String office;

	/**
	 * 
	 */

	private static final long serialVersionUID = -1451746573438651422L;

	public User getUser() {
		String username = (String) getSession().getAttribute("username");
		if (user == null) {			
			user = userDAO.findById(username);
		}
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public DepartmentDAO getDepartmentDAO() {
		return departmentDAO;
	}

	public void setDepartmentDAO(DepartmentDAO departmentDAO) {
		this.departmentDAO = departmentDAO;
	}

	public MajorDAO getMajorDAO() {
		return majorDAO;
	}

	public void setMajorDAO(MajorDAO majorDAO) {
		this.majorDAO = majorDAO;
	}

	public String getDormitory() {
		return dormitory;
	}

	public void setDormitory(String dormitory) {
		this.dormitory = dormitory;
	}

	public String getDepartment() {
		getUser();
		department = departmentDAO.findById(user.getDepartment().getId())
				.getName();
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public String getMajor() {
		getUser();
		major = majorDAO.findById(user.getMajor().getId()).getName();
		return major;
	}

	public void setMajor(String major) {
		this.major = major;
	}

	public String saveStudent() {
		getUser();
		boolean legal = true;
		failReason = "";
		logger.debug("保存学生信息");
		logger.debug("phone:"+phone);
		if (password.length() < 6 || password.length() > 20) {
			if (!failReason.contains("请输入长度6~20的密码")) {
				failReason += "请输入长度6~20的密码";
			}
			legal = false;
		}
		if ((((phone.length() == 12) || (phone.length() == 11)) && (StringUtil
				.isInteger(phone))) || phone.isEmpty()) {
		} else {
			legal = false;
			if (!failReason.contains("</br>请输入正确的电话号码")) {
				failReason += "</br>请输入正确的电话号码";
			}
		}
		if (legal) {
			user.setPassword(password);
			user.setEmail(email);
			user.setPhone(phone);
			user.setDormitory(dormitory);
			userDAO.merge(user);
		}
		return SUCCESS;
	}

	public String saveTeacher() {
		getUser();
		boolean legal = true;
		failReason = "";
		if (password.length() < 6 || password.length() > 20) {
			failReason += "请输入长度6~20的密码";
			legal = false;
		}
		if ((((phone.length() == 12) || (phone.length() == 11)) && (StringUtil
				.isInteger(phone))) || phone.isEmpty()) {
		} else {
			legal = false;
			failReason += "</br>请输入正确的电话号码";
		}
		if (legal) {
			user.setPassword(password);
			user.setEmail(email);
			user.setPhone(phone);
			user.setOffice(office);
			userDAO.merge(user);
		}
		return SUCCESS;
	}

	public String save() {
		String typeString = (String) getSession().getAttribute(KEY_TYPE);
		if (typeString.equals(STUDENT)) {
			saveStudent();
		} else if (typeString.equals(TEACHER)) {
			saveTeacher();
		}
		logger.debug("\n\n\n\n" + failReason);
		logger.debug("傻逼");
		return SUCCESS;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return super.execute();
	}

	public String getFailReason() {
		return StringUtil.urlDecode(failReason);
	}

	public void setFailReason(String failReason) {
		this.failReason = failReason;
	}

	public DegreeDAO getDegreeDAO() {
		getUser();
		degree = degreeDAO.findById(user.getDegree().getId()).getName();
		return degreeDAO;
	}

	public void setDegreeDAO(DegreeDAO degreeDAO) {
		this.degreeDAO = degreeDAO;
	}

	public String getDegree() {
		return degree;
	}

	public void setDegree(String degree) {
		this.degree = degree;
	}

	public String getOffice() {
		return office;
	}

	public void setOffice(String office) {
		this.office = office;
	}

}
